// pages/signFabric/signFabric.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: 'true',
    id: '',
    list: '',
    xlist:'',
    type: '',
    index: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      id: options.id
    })
    this.getOrder()
  },
  getOrder: function() {
    var that = this
    wx.request({
      url: app.globalData.api + 'productionOrderForm/' + this.data.id,
      data: {},
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: (res) => {
        console.log(res)
        if (res.data.code == 200) {
          that.data.list = res.data.data
          for (var i in that.data.list.fabricList) {
            if (that.data.list.fabricList[i].fabricArrearsAmount + that.data.list.fabricList[i].fabricCallBackAmount + that.data.list.fabricList[i].fabricConfirmAmount == 0 && that.data.list.fabricList[i].confirm != 'true') {
              that.data.list.fabricList[i].confirm = ''
            }
          }
          for (var i in res.data.data.auxiliaryFabricList) {
            if (res.data.data.auxiliaryFabricList[i].auxiliaryFabricArrearsAmount + res.data.data.auxiliaryFabricList[i].auxiliaryFabricCallBackAmount + res.data.data.auxiliaryFabricList[i].auxiliaryFabricConfirmAmount == 0 && res.data.data.auxiliaryFabricList[i].confirm != 'true') {
              that.data.list.auxiliaryFabricList[i].confirm = ''
            }
          } 
          that.setData({
            list: that.data.list
          })
          console.log(that.data.list)
        }
      }
    })
  },
  radioChange: function(e) {
    console.log(e)
    this.setData({
      type: e.currentTarget.dataset.type,
      index: e.currentTarget.dataset.index
    })
    if (e.currentTarget.dataset.type == '面料') {
      switch (this.data.list.fabricList[e.currentTarget.dataset.index].confirm) {
        case '':
          this.data.list.fabricList[e.currentTarget.dataset.index].confirm = 'true'
          // this.data.list.fabricList[e.currentTarget.dataset.index].fabricConfirmAmount = 1
          // this.data.list.fabricList[e.currentTarget.dataset.index].fabricArrearsAmount = 1
          // this.data.list.fabricList[e.currentTarget.dataset.index].fabricCallBackAmount = 1
          
          break
        case 'true':
          this.data.list.fabricList[e.currentTarget.dataset.index].confirm = 'false'
          this.setData({
            items: 'false',
          })
          break
        case 'false':
          this.data.list.fabricList[e.currentTarget.dataset.index].confirm = 'true'
          // this.data.list.fabricList[e.currentTarget.dataset.index].fabricConfirmAmount = 1
          // this.data.list.fabricList[e.currentTarget.dataset.index].fabricCallBackAmount = 1
          // this.data.list.fabricList[e.currentTarget.dataset.index].fabricConfirmAmount = 1
          break
      }

    } else if (e.currentTarget.dataset.type == '辅料') {
      switch (this.data.list.auxiliaryFabricList[e.currentTarget.dataset.index].confirm) {
        case '':
          this.data.list.auxiliaryFabricList[e.currentTarget.dataset.index].confirm = 'true'
          // this.data.list.auxiliaryFabricList[e.currentTarget.dataset.index].auxiliaryFabricConfirmAmount = 1
          // this.data.list.auxiliaryFabricList[e.currentTarget.dataset.index].auxiliaryFabricCallBackAmount = 1
          // this.data.list.auxiliaryFabricList[e.currentTarget.dataset.index].auxiliaryFabricArrearsAmount = 1
          break
        case 'true':
          this.data.list.auxiliaryFabricList[e.currentTarget.dataset.index].confirm = 'false'
          this.setData({
            items: 'false',
          })
          break
        case 'false':
          this.data.list.auxiliaryFabricList[e.currentTarget.dataset.index].confirm = 'true'
          // this.data.list.auxiliaryFabricList[e.currentTarget.dataset.index].auxiliaryFabricConfirmAmount = 1
          // this.data.list.auxiliaryFabricList[e.currentTarget.dataset.index].auxiliaryFabricArrearsAmount = 1
          // this.data.list.auxiliaryFabricList[e.currentTarget.dataset.index].auxiliaryFabricCallBackAmount = 1
          break
      }
    }
    this.setData({
      list: this.data.list
    })
  },
  Submit: function(e) {
    if (e.detail.value.remark == '') {
      wx.showModal({
        title: '提示',
        content: '请填写拒收原因',
      })
      return
    } else if (e.detail.value.rejectionNum == '' && e.detail.value.returnNum == '') {
      wx.showModal({
        title: '提示',
        content: '请填写缺货数量或退回量',
      })
      return
    }
    if (this.data.type == '面料') {
      this.data.list.fabricList[this.data.index].fabricCallBackRemark = e.detail.value.remark
      this.data.list.fabricList[this.data.index].fabricArrearsAmount = e.detail.value.rejectionNum
      this.data.list.fabricList[this.data.index].fabricCallBackAmount = e.detail.value.returnNum
    } else {
      this.data.list.auxiliaryFabricList[this.data.index].auxiliaryFabricCallBackRemark = e.detail.value.remark
      this.data.list.auxiliaryFabricList[this.data.index].auxiliaryFabricArrearsAmount = e.detail.value.rejectionNum
      this.data.list.auxiliaryFabricList[this.data.index].auxiliaryFabricCallBackAmount = e.detail.value.returnNum
    }
    this.setData({
      items: 'true'
    })
    console.log(this.data.list)

  },
  mb: function() {

    if (this.data.type == '面料') {
      this.data.list.fabricList[this.data.index].confirm = 'true'
    } else {
      this.data.list.auxiliaryFabricList[this.data.index].confirm = 'true'
    }
    this.setData({
      items: 'true',
      list: this.data.list
    })
  },
  sub: function() {
    for (var i in this.data.list.fabricList ){
      if (this.data.list.fabricList[i].confirm == ''){
        wx.showToast({
          title: '请确认签收',
        })
        return
      }
    }
    for (var i in this.data.list.auxiliaryFabricList) {
      if (this.data.list.auxiliaryFabricList[i].confirm == '') {
        wx.showToast({
          title: '请确认签收',
        })
        return
      }
    }
    wx.request({
      url: app.globalData.api + 'productionOrderForm/confirmFabric/' + this.data.id,
      data: {
        fabricList: this.data.list.fabricList,
        auxiliaryFabricList: this.data.list.auxiliaryFabricList,
        purchasingOrderFormId: this.data.list.purchasingOrderFormId
      },
      header: {
        Authorization: wx.getStorageSync('token')
      },
      method: 'PUT',
      success: (res) => {
        console.log(res)
        if (res.data.code == 200) {
          wx.showToast({
            title: res.data.msg,
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 1000)
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.errorMsg,
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})